package cn.itsource.exam.service.impl;

import cn.itsource.exam.domain.LogInfo;
import cn.itsource.exam.domain.SubjectInfo;
import cn.itsource.exam.enums.LogOperateTypeEnum;
import cn.itsource.exam.mapper.SubjectInfoMapper;
import cn.itsource.exam.query.SubjectQuery;
import cn.itsource.exam.service.SubjectService;
import cn.itsource.exam.util.PageList;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectInfoMapper subjectMapper;

    @Autowired
    private LogInfoServiceImpl logInfoService;

    /**
     * 新增或修改学科信息
     * @param paramIn
     * @param name
     * @return
     */
    @Override
    public String modifySubject(SubjectInfo paramIn, String name) {
        int count = 0;
        if(paramIn.getId() != null){ // 修改
            paramIn.setModifytime(new Date());
            count = subjectMapper.updateById(paramIn);
        }else{
            paramIn.setAddtime(new Date());
            paramIn.setStatus(0);
            count = subjectMapper.insert(paramIn);
        }
        return count > 0 ? "":"操作失败";
    }

    /**
     * 删除学科
     * @param id
     * @param name
     * @return
     */
    @Override
    public String delSubject(Long id, String name) {
        SubjectInfo subjectInfo = subjectMapper.selectById(id);
        subjectInfo.setStatus(1);
        subjectInfo.setModifytime(new Date());
        int count = subjectMapper.updateById(subjectInfo);
        if(count > 0){
            logInfoService.addLogInfo(new LogInfo(LogOperateTypeEnum.SUBJECT.getId(),"删除"+subjectInfo.getSubjectname(),name));
            return "";
        }
        return "操作失败";
    }

    /**
     * 批量删除学科
     * @param ids
     * @param name
     * @return
     */
    @Override
    public String batchDelSubject(String ids, String name) {
        if(StringUtils.isEmpty(ids)){
            return "请选择要删除的数据!";
        }
        List<SubjectInfo> list = subjectMapper.selectBatchIds(Arrays.asList(ids.split(",")));
        int count = subjectMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        if(count > 0){
            new Thread(){   // 异步插入日志
                public void run(){
                    for(SubjectInfo user:list){
                        logInfoService.addLogInfo(new LogInfo(LogOperateTypeEnum.SUBJECT.getId(),"删除"+user.getSubjectname()+"信息",name));
                    }
                }
            }.start();
        }
        return count > 0 ? "":"操作失败";
    }

    // 根据主键获取学科信息
    @Override
    public SubjectInfo getSubjectById(Long id) {
        return subjectMapper.selectById(id);
    }

    /**
     * 学科分页查询
     * @param paramIn
     * @return
     */
    @Override
    public PageList<SubjectInfo> getSubjectListPage(SubjectQuery paramIn) {
        Integer count = subjectMapper.countSubJectList(paramIn);
        if (count == null || count == 0){
            return  new PageList<SubjectInfo>();
        }
        PageList<SubjectInfo> list =  new PageList<SubjectInfo>(count,subjectMapper.selectSubjectList(paramIn));
        return list;
    }

    @Override
    public List<SubjectInfo> getSubjectList() {
        return subjectMapper.selectAllSubject();
    }
}
